package com.qf.service.Impl;/*
    Dell
    2021/10/9
    11:45
*/

import com.qf.dao.Impl.ProductDaoImpl;
import com.qf.dao.ProductDao;
import com.qf.entiy.Product;
import com.qf.service.ProductService;
import com.qf.util.DruidUtil;
import com.qf.util.Page;

import java.util.List;

public class ProductServiceImpl implements ProductService {
    private ProductDao productDao = new ProductDaoImpl();
    @Override
    public List<Product> showAll(Page page) {
        List<Product> productList = null;
        Long aLong = productDao.countProduct();
        page.setTotalCount(aLong);
        productList =  productDao.showAll(page);
        DruidUtil.close();
        return productList;
    }

    @Override
    public Integer add(Product product, Integer cid) {
        int result = 0;
        try {
            DruidUtil.begin();
            result = productDao.insert(product, cid);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }
        return result;
    }

    @Override
    public Integer updateProduct(Product product, Integer cid) {
        int result = 0;
        try {
            DruidUtil.begin();
            result = productDao.update(product, cid);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }
        return result;
    }

    @Override
    public Integer delete(Integer id) {
        int result = 0;
        try {
            DruidUtil.begin();
            result = productDao.delete(id);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }
        return result;
    }
}
